package com.xnyzc.lhy.mis.service.impl.worksheet;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.entity.DiDiRv;
import com.xnyzc.lhy.common.exception.WorksheetException;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.penum.worksheet.WorksheetErrorCode;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.mis.entity.worksheet.OaSysWorksheetOperate;
import com.xnyzc.lhy.mis.mapper.worksheet.OaSysWorksheetOperateMapper;
import com.xnyzc.lhy.mis.service.worksheet.IOaSysWorksheetOperateService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 工单交互 服务实现类
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-11-04
 */
@Service
@Slf4j
public class OaSysWorksheetOperateServiceImpl extends ServiceImpl<OaSysWorksheetOperateMapper, OaSysWorksheetOperate> implements IOaSysWorksheetOperateService {

    @Autowired
    OaSysWorksheetOperateMapper oaSysWorksheetOperateMapper;

    /**
     * 创建操作记录
     * @param param
     * @param worksheetId
     * @param actName
     * @param fromSource
     * @param toSource
     * @return
     */
    @Override
    public OaSysWorksheetOperate createWorksheetOperate(Object param,Long worksheetId ,String actName, String fromSource,String toSource){
        try {
            OaSysWorksheetOperate sheetOperate = new OaSysWorksheetOperate();
            Long operateId = IDUtil.nextId();
            sheetOperate.setId(operateId);
            sheetOperate.setWorksheetId(worksheetId);
            sheetOperate.setSourceId(0L);
            sheetOperate.setName(actName);
            sheetOperate.setFromSource(fromSource);
            sheetOperate.setToSource(toSource);
            sheetOperate.setBeginTime(new Date());
            sheetOperate.setEndTime(new Date());
            sheetOperate.setData(String.valueOf(JSONObject.fromObject(param)));
            sheetOperate.setResult("");
            oaSysWorksheetOperateMapper.insert(sheetOperate);

            return sheetOperate;
        }catch (Exception ex){
            log.info("创建工单接口调用记录异常");
            return null;
        }
    }

    /**
     * 创建合力接口操作
     * @param param
     * @param worksheetId
     * @param actName
     * @return
     */
    @Override
    public OaSysWorksheetOperate createHeliWorksheetOperate(Object param, Long worksheetId, String actName){
        //创建合力调用时的接口操作记录
        return  this.createWorksheetOperate(param,
                worksheetId,
                actName,
                "hollycrm",
                "didi");
    }

    /**
     * 更新操作记录
     * @param operateId
     */
    @Override
    public void updateWorksheetOperate(Long operateId, Object result) {
        try {
            // 更新调用记录
            OaSysWorksheetOperate operate = oaSysWorksheetOperateMapper.selectById(operateId);
            operate.setEndTime(new Date());
            operate.setResult(String.valueOf(JSONObject.fromObject(result)));
            oaSysWorksheetOperateMapper.updateById(operate);
        } catch (Exception ex) {
            log.info("更新工单接口调用记录异常");
            return;
        }
    }
}
